package com.project.controller;

import com.project.bean.ManBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("man")
public class ManController {
    private static List<ManBean> manList = new ArrayList<>();
    static{
        manList.add(new ManBean(3,"郭靖", LocalDate.parse("1998-03-02")));
        manList.add(new ManBean(6,"段正淳", LocalDate.parse("1986-06-12")));
        manList.add(new ManBean(2,"郭啸天", LocalDate.parse("1995-12-31")));
        manList.add(new ManBean(5,"虚竹", LocalDate.parse("1996-09-15")));
        manList.add(new ManBean(9,"郭嘉", LocalDate.parse("1984-12-09")));
        manList.add(new ManBean(8,"郭破虏", LocalDate.parse("1992-11-05")));
    }


    @RequestMapping("findByItem")
    public List findByItem(String name){
        if(name == null){
            return manList;
        }

        List<ManBean> mlist = new ArrayList<>();
        for(ManBean man : manList){
            if(man.getName().contains(name)){
                mlist.add(man);
            }
        }

        return mlist;
    }

    @RequestMapping("del")
    public String del(int id){
        manList.remove(new ManBean(id));
        return "ok";
    }

    @RequestMapping("add")
    public String add(ManBean man){
        manList.add(man);
        return "ok";
    }

    @RequestMapping("findById")
    public ManBean findById(int id ){
        int index = manList.indexOf(new ManBean(id));

        return manList.get(index);
    }

    @RequestMapping("update")
    public String update(int id,LocalDate birthday){
        this.findById(id).setBirthday(birthday);
        return "ok";
    }
}
